package de.koller.gravity.render;

import de.koller.gravity.GNode;
import de.koller.oceanFramework.gl.util.ImagePreview;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

public class GRender extends GNode {
	
	public boolean renderQuality = false;
	public boolean renderQualitySequence = false;
	private int lastElapsedTime;

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		lastElapsedTime = elapsedTime;
	}
	
	public void renderThis( Graphics2D g ) {
		groot.scene.renderSceneFast( g, lastElapsedTime );
		
		if( renderQuality )
			renderQuality();
		if( renderQualitySequence )
			renderQualitySequence();
	}
	
	
	private void renderQualitySequence() {
		String fn = JOptionPane.showInputDialog( null, "Name des Ordner:", "Sequence", JOptionPane.PLAIN_MESSAGE );
		String nu = JOptionPane.showInputDialog( null, "Anzahl an Bildern:", "Sequence", JOptionPane.PLAIN_MESSAGE );
		
		File f = new File( "./screenshots/output/" + fn + "/" );
		f.mkdirs();
		
		groot.scene.initSequence();
		
		int a = Integer.parseInt( nu );
		for( int i = 0; i < a; i++ ) {
			System.out.println( "render img: " + i + " / " + a );
			BufferedImage img = groot.scene.renderScene();
			try {
				ImageIO.write( img, "png", new File( "./screenshots/output/" + fn + "/" + i + ".png" ) );
			} catch( IOException ex ) {
				System.err.println( ex );
			}
		}
		
		renderQualitySequence = false;
	}
	
	private void renderQuality() {
		BufferedImage img = groot.scene.renderScene();
		
		try {
			ImageIO.write( img, "png", new File( "./screenshots/grav_screen.png" ) );
		} catch( IOException ex ) {
			System.err.println( ex );
		}
		
		ImagePreview.showImage( img, img.getWidth(), img.getHeight() );
		renderQuality = false;
	}
	
}
